
package dialogs;

import core.COINCOMOComponent;
import database.COINCOMOComponentManager;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import main.GlobalMethods;
import main.Icons;
import panels.ComponentOverviewPanel;

/**
 *
 * @author Raed Shomali
 */
public class ComponentScaleFactorsDialog extends JDialog implements ActionListener
{
    JPopupMenu ratingPopup = new JPopupMenu();

    JPopupMenu incrementPopup = new JPopupMenu();

    JMenuItem vloMenuItem = new JMenuItem( "VLO" );

    JMenuItem loMenuItem = new JMenuItem( "LO" );

    JMenuItem nomMenuItem = new JMenuItem( "NOM" );

    JMenuItem hiMenuItem = new JMenuItem( "HI" );

    JMenuItem vhiMenuItem = new JMenuItem( "VHI" );

    JMenuItem xhiMenuItem = new JMenuItem( "XHI" );

    JMenuItem zeroMenutItem = new JMenuItem( "0%" );

    JMenuItem twentyFiveMenutItem = new JMenuItem( "25%" );

    JMenuItem fiftyMenutItem = new JMenuItem( "50%" );

    JMenuItem seventyFiveMenutItem = new JMenuItem( "75%" );

    JButton ratingButtons[] =
    {
        new JButton( "NOM" ) , new JButton( "NOM" ) , new JButton( "NOM" ) , new JButton( "NOM" ) , new JButton( "NOM" )
    };

    JButton incrementButtons[] =
    {
        new JButton( "0%" ) , new JButton( "0%" ) , new JButton( "0%" ) , new JButton( "0%" ) , new JButton( "0%" )
    };

    JButton applyButton = new JButton( "Apply" );

    JButton resetButton = new JButton( "Reset" );

    JButton closeButton = new JButton( "Close" );
    // The Button From Which the Popup Generated ...
    JButton ownerButton = null;

    COINCOMOComponent component = null ;
    
    public ComponentScaleFactorsDialog( JFrame owner , COINCOMOComponent component )
    {
        super( owner );

        this.setModal( true );

        this.component = component ;
        
        this.setTitle( "Scale Factors" );

        GlobalMethods.updateStatusBar( "Done." );

        // Popup
        ratingPopup.add( vloMenuItem );
        ratingPopup.add( loMenuItem );
        ratingPopup.add( nomMenuItem );
        ratingPopup.add( hiMenuItem );
        ratingPopup.add( vhiMenuItem );
        ratingPopup.add( xhiMenuItem );

        incrementPopup.add( zeroMenutItem );
        incrementPopup.add( twentyFiveMenutItem );
        incrementPopup.add( fiftyMenutItem );
        incrementPopup.add( seventyFiveMenutItem );

        // Menu Items
        vloMenuItem.addActionListener( this );
        loMenuItem.addActionListener( this );
        nomMenuItem.addActionListener( this );
        hiMenuItem.addActionListener( this );
        vhiMenuItem.addActionListener( this );
        xhiMenuItem.addActionListener( this );

        zeroMenutItem.addActionListener( this );
        twentyFiveMenutItem.addActionListener( this );
        fiftyMenutItem.addActionListener( this );
        seventyFiveMenutItem.addActionListener( this );

        // Buttons
        applyButton.addActionListener( this );
        resetButton.addActionListener( this );
        closeButton.addActionListener( this );

        applyButton.setFocusable( false );
        resetButton.setFocusable( false );
        closeButton.setFocusable( false );

        applyButton.setIcon( Icons.SAVE_ICON );
        resetButton.setIcon( Icons.RESET_ICON );
        closeButton.setIcon( Icons.CLOSE_ICON );

        // GUI
        this.setLayout( null );

        JLabel ratingLabel = new JLabel( "Rating" );
        JLabel incrementLabel = new JLabel( "Increment" );

        ratingLabel.setFont( new Font( "arial" , 1 , 11 ) );
        incrementLabel.setFont( new Font( "arial" , 1 , 11 ) );

        ratingLabel.setHorizontalAlignment( JLabel.CENTER );
        incrementLabel.setHorizontalAlignment( JLabel.CENTER );

        this.add( ratingLabel );
        this.add( incrementLabel );

        ratingLabel.setBounds( 200 , 20 , 65 , 25 );
        incrementLabel.setBounds( 275 , 20 , 65 , 25 );

        JLabel label1 = new JLabel( "Precedentedness" );
        JLabel label2 = new JLabel( "Development Flexibility" );
        JLabel label3 = new JLabel( "Risk Resolution" );
        JLabel label4 = new JLabel( "Team Cohesion" );
        JLabel label5 = new JLabel( "Process Maturity" );

        label1.setBounds( 10 , 50 , 200 , 25 );
        label2.setBounds( 10 , 85 , 200 , 25 );
        label3.setBounds( 10 , 120 , 200 , 25 );
        label4.setBounds( 10 , 155 , 200 , 25 );
        label5.setBounds( 10 , 190 , 200 , 25 );

        label1.setFont( new Font( "arial" , 1 , 11 ) );
        label2.setFont( new Font( "arial" , 1 , 11 ) );
        label3.setFont( new Font( "arial" , 1 , 11 ) );
        label4.setFont( new Font( "arial" , 1 , 11 ) );
        label5.setFont( new Font( "arial" , 1 , 11 ) );

        this.add( label1 );
        this.add( label2 );
        this.add( label3 );
        this.add( label4 );
        this.add( label5 );

        for ( int i = 0 ; i < 5 ; i++ )
        {
            JButton tempRatingButton = ratingButtons[i];
            JButton tempIncrementButton = incrementButtons[i];

            tempRatingButton.setText( GlobalMethods.convertToRating( component.getScaleFactorsBase()[ i ] ) ) ;
            tempIncrementButton.setText( GlobalMethods.convertToPercent( component.getScaleFactorsIncr()[ i ] ) ) ;
            
            tempRatingButton.setFont( new Font( "courier" , 1 , 16 ) );
            tempIncrementButton.setFont( new Font( "courier" , 1 , 16 ) );

            tempRatingButton.setFocusable( false );
            tempIncrementButton.setFocusable( false );

            tempRatingButton.addActionListener( this );
            tempIncrementButton.addActionListener( this );

            this.add( tempRatingButton );
            this.add( tempIncrementButton );

            tempRatingButton.setBounds( 200 , 50 + 35 * i , 65 , 25 );
            tempIncrementButton.setBounds( 275 , 50 + 35 * i , 65 , 25 );
        }

        // Panel
        JPanel southPanel = new JPanel();
        southPanel.add( applyButton );
        southPanel.add( resetButton );
        southPanel.add( closeButton );

        this.add( southPanel );

        southPanel.setBounds( 0 , 240 , 360 , 40 );

        this.setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
        this.setLocation( this.getOwner().getX() + 100 , this.getOwner().getY() + 100 );
        this.setResizable( false );
        this.setSize( 360 , 320 );
        this.setVisible( true );
    }

    public void actionPerformed( ActionEvent e )
    {
        if ( e.getSource() == applyButton )
        {
            ComponentOverviewPanel.scaleFactorsButton.setEnabled( false ) ;
            
            applyButton.setEnabled( false ) ;
            resetButton.setEnabled( false ) ;
            closeButton.setEnabled( false ) ;
            
            GlobalMethods.updateStatusBar( "Saving ..." ) ;
            
            SwingUtilities.invokeLater( new Runnable()
            {
                public void run()
                {
                    int scaleFactorsBase[] = new int[ 5 ] ;
                    int scaleFactorsIncr[] = new int[ 5 ] ;
                    
                    for( int i = 0 ; i < ratingButtons.length ; i ++ )
                    {
                        scaleFactorsBase[ i ] = GlobalMethods.convertFromRating( ratingButtons[ i ].getText() ) ;
                        scaleFactorsIncr[ i ] = GlobalMethods.convertFromPercent( incrementButtons[ i ].getText() ) ;
                    }
                    
                    component.setScaleFactorsBase( scaleFactorsBase ) ;
                    component.setScaleFactorsIncr( scaleFactorsIncr ) ;
                    
                    // Update Database ..
                    COINCOMOComponentManager.updateComponent( component ) ;
                    
                    // Update Button ...
                    ComponentOverviewPanel.scaleFactorsButton.setText( "Scale Factors " + component.getScaleFactor() ) ;
                    
                    // Update Summary Report
                    ComponentOverviewPanel.updateEstimationTextPane( true );
                    
                    GlobalMethods.updateStatusBar( "Scale Factors Saved." ) ;
                    
                    // Back To Default
                    applyButton.setEnabled( true ) ;
                    resetButton.setEnabled( true ) ;
                    closeButton.setEnabled( true ) ;
                    
                    ComponentOverviewPanel.scaleFactorsButton.setEnabled( true ) ;
                }
            });
        }
        else if ( e.getSource() == resetButton )
        {
            for ( int i = 0 ; i < 5 ; i++ )
            {
                JButton tempRatingButton = ratingButtons[i];
                JButton tempIncrementButton = incrementButtons[i];

                tempRatingButton.setText( "NOM" );
                tempIncrementButton.setText( "0%" );
            }

        }
        else if ( e.getSource() == closeButton )
        {
            // Free Resources ... Close Window
            this.dispose();
        }
        else if ( e.getSource() == vloMenuItem || e.getSource() == loMenuItem || e.getSource() == nomMenuItem ||
                e.getSource() == hiMenuItem || e.getSource() == vhiMenuItem || e.getSource() == xhiMenuItem )
        {
            ownerButton.setText( ((JMenuItem) e.getSource()).getText() );
        }
        else if ( e.getSource() == zeroMenutItem || e.getSource() == twentyFiveMenutItem ||
                e.getSource() == fiftyMenutItem || e.getSource() == seventyFiveMenutItem )
        {
            ownerButton.setText( ((JMenuItem) e.getSource()).getText() );
        }
        else
        {
            ownerButton = ((JButton) e.getSource());

            if ( ownerButton.getText().endsWith( "%" ) )
            {
                incrementPopup.show( (Component) e.getSource() , ((JButton) e.getSource()).getWidth() / 2 , ((JButton) e.getSource()).getHeight() / 2 );
            }
            else
            {
                ratingPopup.show( (Component) e.getSource() , ((JButton) e.getSource()).getWidth() / 2 , ((JButton) e.getSource()).getHeight() / 2 );
            }
        }
    }

}
